package com.woniuxy.background.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.woniuxy.background.realm.CustomRealm;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public CustomRealm customRealm(MemoryConstrainedCacheManager cacheManager){
        CustomRealm realm = new CustomRealm();
        realm.setCacheManager(cacheManager);
        return realm;
    }
    //配置安全管理器
    @Bean
    public DefaultWebSecurityManager securityManager(CustomRealm customRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(customRealm);
        return securityManager;
    }
    //配置过滤器
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager manager){
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        factoryBean.setSecurityManager(manager);
        factoryBean.setLoginUrl("/login");
        Map map = new LinkedHashMap<String,String>();
        map.put("/admin/subLogin","anon");
        map.put("/login","anon");
        /*map.put("/app/**","anon");
        map.put("/css/**","anon");
        map.put("/font/**","anon");
        map.put("/images/**","anon");
        map.put("/lay/**","anon");
        map.put("/zTree_v3/**","anon");
        map.put("/jquery-1.9.1.min.js","anon");
        map.put("/layui.js","anon");*/
        map.put("/src/**", "anon");
        map.put("/logout","logout");
        map.put("/**","authc");
        factoryBean.setFilterChainDefinitionMap(map);
        return factoryBean;
    }

    //缓存管理器
    @Bean
    public MemoryConstrainedCacheManager cacheManager(){
        return new MemoryConstrainedCacheManager();
    }



    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator creator(){
        DefaultAdvisorAutoProxyCreator creator =new DefaultAdvisorAutoProxyCreator();
        creator.setProxyTargetClass(true);
        return creator;
    }



}
